package logic

import (
	"context"
	"time"

	"gitee.com/gao-deguang/easy-chat/apps/im/models"
	"gitee.com/gao-deguang/easy-chat/apps/im/ws/internal/svc"
	"gitee.com/gao-deguang/easy-chat/apps/im/ws/websocket"
	"gitee.com/gao-deguang/easy-chat/apps/im/ws/ws"
	"gitee.com/gao-deguang/easy-chat/pkg/wuid"
)

type Conversation struct {
	ctx context.Context
	svr *websocket.Server
	svc *svc.ServiceContext
}

func NewConversation(ctx context.Context, svr *websocket.Server, svc *svc.ServiceContext) *Conversation {
	return &Conversation{
		ctx: ctx,
		svr: svr,
		svc: svc,
	}
}

func (l *Conversation) SingleChat(data *ws.Chat, userID string) error {
	if data.ConversationId == "" {
		data.ConversationId = wuid.CombineId(userID, data.RecvId)
	}

	chatLog := models.ChatLog{
		ConversationId: data.ConversationId,
		ChatType:       data.ChatType,
		SendId:         userID,
		RecvId:         data.RecvId,
		MsgFrom:        0,
		MsgType:        data.Mtype,
		MsgContent:     data.Content,
		SendTime:       time.Now().UnixNano(),
	}

	l.svr.Infof("执行插入操作执行插入操作执行插入操作执行插入操作执行插入操作执行插入操作")
	err := l.svc.ChatLogModel.Insert(l.ctx, &chatLog)
	if err != nil {
		l.svr.Infof("插入错误插入错误插入错误插入错误插入错误插入错误Err:%v", err)
	}
	return err
}
